package com.zhny.protocol_v3_008.network;

import java.io.*;
import java.net.Socket;
import java.util.Properties;

/**
 * 建立长连接，实例化Socket,开启输入输出流
 * @author Create By Alex On 2018/4/8 14:41
 */
public abstract class AbsNetwork {
    private static Socket socket;
    static InputStream is;
    static OutputStream os;

    private static String host;
    private static int port;

    static {
        Properties properties = new Properties();
        try {
            properties.load(AbsNetwork.class.getClassLoader().getResourceAsStream("config.properties"));
            host = properties.getProperty("network.host");
            port = Integer.parseInt(properties.getProperty("network.port"));
            socket = new Socket(host,port);
            os = socket.getOutputStream();
            is = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    protected void close() {
        if (is != null) {
            try {
                is.close();
                if (os != null)
                    os.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }
}
